package com.qianfeng.qlm.handforireader.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

/**
 * Created by IntelliJ IDEA
 * User:tadhg
 * Date:2015/3/25
 * Email:tadhg299@163.com
 */
public class MyScrollView extends ScrollView {

    private boolean isCanMove;

    private float distanceX , distanceY , lastX , lastY;

    public MyScrollView(Context context) {
        super(context);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setCanMove(boolean isCanMove) {
        this.isCanMove = isCanMove;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                distanceX = distanceY = 0f;
                lastX = ev.getX();
                lastY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float currentX = ev.getX();
                float currentY = ev.getY();

                distanceX = Math.abs(currentX - lastX);
                distanceY = Math.abs(currentY - lastY);

                lastX = currentX;
                lastY = currentY;
                if (distanceX > distanceY){
                    return false;
                }


        }

        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        if (!isCanMove){
            return false;
        }

        return super.onTouchEvent(ev);
    }
}
